package org.tong.jackson;

import org.tong.jpa.entities.Student;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author
 * @date 2022-08-12 10:39
 */
public class JacksonMain {
    public static void main(String[] args) {
        Student student = new Student();
        student.setId(1L);
        student.setName("student1");
        student.setVersion(1);

        // bean
        String json = JsonUtil.toJson(student);
        System.out.println("json: " + json);

        Student s2 = JsonUtil.toData(json, Student.class);
        System.out.println("obj:" + s2);

        // list
        List<Student> students = IntStream.range(1, 5).mapToObj(idx -> {
            Student stu = new Student();
            stu.setId(idx * 1L);
            stu.setName("stu" + idx);
            stu.setVersion(1);
            return stu;
        }).collect(Collectors.toList());
        String jsons = JsonUtil.toJson(students);
        System.out.println("jsons:" + jsons);
        List<Student> stus = JsonUtil.toData(jsons, List.class);
        System.out.println("objs:" + stus);
    }


}
